package com.exp.bookhouse.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.exp.bookhouse.pojo.Permission;
import com.exp.bookhouse.pojo.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface RoleMapper extends BaseMapper<Role> {

    @Select("select r.* from tb_user u, tb_user_role ur, tb_role r where u.login_user = #{userName} and u.id = ur.user_id and ur.role_id = r.id")
    List<Role> queryRolesByUserName(String userName);

    IPage<Role> queryRolesByUserIdAndPage(IPage<Role> iPage, @Param("id") Long id);

    Role queryRoleById(Long id);

    @Select("select *from tb_permission where id not in (select permission_id from tb_role_permission where role_id = #{roleId})")
    List<Permission> queryOtherPermissions(Long roleId);

    @Insert("insert into tb_role_permission (role_id, permission_id) values(#{roleId}, #{permissionId})")
    void saveRoleAddPermission(@Param("roleId") Long roleId, @Param("permissionId") long id);


    @Delete("delete from tb_role_permission where role_id = #{id}")
    void deleteRolePermissionByRoleId(Long id);

    @Select("select *from tb_permission where id in (select permission_id from tb_role_permission where role_id = #{id})")
    List<Permission> queryPermissionsById(Long id);

    @Delete("delete from tb_user_role where role_id = #{id}")
    void deleteUserRoleByRoleId(Long id);
}
